package com.centerm.bluetooth.model;

import android.support.annotation.IntRange;

import com.centerm.bluetooth.ibridge.constants.ChannelConfig;

/**
 * Created by linwenhui on 2017/2/8.
 */

public enum RespondStatus {
    OK(0, "处理成功"), COMMAND_NO_SUPPORT(1, "指令码不支持"), ERROR_PARAM(2, "参数错误"), ERROR_PARAM_LEN(3, "可变数据域长度错误"),
    ERROR_FRAME(4, "帧格式错误"), ERROR_LRC(5, "LRC 交易失败"), ERROR_OTHER(6, "其他"), TIMEOUT(7, "超时"), ERROR_STATUS(8, "返回当前状态"),
    ERROR_NO_COMPLETION(9, "接收报文不完整"), ERROR_LEN(10, "报文的长度比实际数据长度短"), ERROR_VERIFY(11, "校验和错误"), ERROR_HEAD_FORMAT(12, "包头格式出错"),
    ERROR_CMD_NO_FIT(13, "命令不匹配"), ERROR_LASTER_FORMAT(14, "结束符不匹配"), ERROR_PACKAGER_ID(15, "包序号不匹配"),
    ERROR_MAIN_KEY(1, "主密钥不存在"), ERROR_WORK_KEY(2, "工作密钥不存在"), ERROR_CHECKVALUE(3, "CHECKVALUE错误"), ERROR_PARAM2(4, "参数错误"),
    ERROR_VAR_LEN(5, "可变数据域长度错误"), ERROR_FRAME2(6, "帧格式错误"), ERROR_RUN(7, "执行异常"), ERROR_DATABASE(8, "数据库操作失败"),
    ERROR_PRINT(9, "无打印机"), ERROR_UNKNOWN(10, "未知指令"), ERROR_LRC2(11, "LRC校验失败"), ERROR_TIME_OUT(12, "交易超时"), ERROR_OTHER2(13, "其它"),
    ERROR_LOCK(14, "终端锁机"), ERROR_PARAM_UNSUPPORT(15, "暂不支持该参数");
    private int status;
    private String msg;

    RespondStatus(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    /**
     *
     * @param code
     * @param type 0 拉卡拉版，1 通用版
     * @return
     */
    public static RespondStatus valueOf(@IntRange int code, int type) {
        if (type == ChannelConfig.LKL) {
            RespondStatus[] respondStatuses = new RespondStatus[]{COMMAND_NO_SUPPORT, ERROR_PARAM, ERROR_PARAM_LEN, ERROR_FRAME, ERROR_LRC, ERROR_OTHER, TIMEOUT, ERROR_STATUS};
            for (RespondStatus respondStatus : respondStatuses) {
                if (respondStatus.getStatus() == code) {
                    return respondStatus;
                }
            }
        } else if (type == ChannelConfig.CENTERM) {
            RespondStatus[] respondStatuses = new RespondStatus[]{ERROR_MAIN_KEY, ERROR_WORK_KEY, ERROR_CHECKVALUE, ERROR_PARAM2,
                    ERROR_VAR_LEN, ERROR_FRAME2, ERROR_RUN, ERROR_DATABASE, ERROR_PRINT, ERROR_UNKNOWN, ERROR_LRC2, ERROR_TIME_OUT,
                    ERROR_OTHER2, ERROR_LOCK, ERROR_PARAM_UNSUPPORT};
            for (RespondStatus respondStatus : respondStatuses) {
                if (respondStatus.getStatus() == code) {
                    return respondStatus;
                }
            }
        }
        return ERROR_OTHER;
    }
}
